package cz.muni.fi.pa165.languageschool.dao.impl;

import cz.muni.fi.pa165.languageschool.Lecturer;
import cz.muni.fi.pa165.languageschool.dao.LecturerDao;
import cz.muni.fi.pa165.languageschool.utils.Authority;
import java.util.Collection;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * LectureDao implementation using JPA
 *
 * @author Michal Fučík (395624) michal.fuca.fucik(at)gmail.com
 */
public class LecturerDaoImpl implements LecturerDao {

    final static Logger log = LoggerFactory.getLogger(LecturerDaoImpl.class);

    private EntityManager entityManager;

    public LecturerDaoImpl() {
    }

    public LecturerDaoImpl(EntityManager em) {
        if (em == null) {
            throw new NullPointerException("Argument em was null");
        }
        this.entityManager = em;
    }

    public EntityManager getEntityManager() {
        return entityManager;
    }

    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    @Override
    public void createLecturer(Lecturer lecturer) {
        log.info("***** DAO Lecturer - createLecturer: {}", lecturer);
        if (lecturer == null) {
            throw new NullPointerException("Argument lecturer cannot be null.");
        }
        entityManager.persist(lecturer);
    }

    @Override
    public void updateLecturer(Lecturer lecturer) {
        log.info("***** DAO Lecturer - updateLecturer: {}", lecturer);
        if (lecturer == null) {
            throw new NullPointerException("Argument lecturer cannot be null.");
        }

        Lecturer lrr = entityManager.find(Lecturer.class, lecturer.getId());
        if (lrr == null) {
            throw new IllegalArgumentException("Entity does not exist in db");
        }

        lrr.setFirstName(lecturer.getFirstName());
        lrr.setLastName(lecturer.getLastName());
        lrr.setNativeSpeaker(lecturer.getNativeSpeaker());
        lrr.setUsername(lecturer.getUsername());
        lrr.setPassword(lecturer.getPassword());
        lrr.setAuthority(Authority.ROLE_ADMIN);
        lrr.setLanguages(lecturer.getLanguages());
        lrr.setEnabled(true);

        entityManager.persist(lrr);
    }

    @Override
    public void deleteLecturer(Lecturer lecturer) {
        log.info("***** DAO Lecturer - deleteLecturer: {}", lecturer);
        if (lecturer == null) {
            throw new NullPointerException("Argument lecturer cannot be null.");
        }

        Lecturer lrr = entityManager.find(Lecturer.class, lecturer.getId());
        if (lrr == null) {
            throw new IllegalArgumentException("Passed entity doesn't exist in database");
        }

        entityManager.remove(lrr);
    }

    @Override
    public Lecturer getLecturer(Long id) {
        log.info("***** DAO Lecturer - getLecturer: {}", id);
        Lecturer res = null;
        if (id == null) {
            throw new NullPointerException("Argument lecturer id cannot be null.");
        }

        return entityManager.find(Lecturer.class, id);
    }

    @Override
    public Collection<Lecturer> getAllLecturers() {
        log.info("***** DAO Lecturer - getAllUsers");

        TypedQuery<Lecturer> tq = entityManager.createQuery(
                "SELECT l FROM Lecturer l", Lecturer.class);
        return tq.getResultList();
    }

    @Override
    public Lecturer findByUsername(String username) {
        log.info("***** DAO Lecturer - find by username: {}", username);
        Lecturer res = null;
        if (username == null) {
            throw new NullPointerException("Passed argument id was null");
        }

        TypedQuery<Lecturer> tq = entityManager.createQuery(
                "SELECT l FROM Lecturer l WHERE l.username LIKE ?1", Lecturer.class).setParameter(1, username);

        if (!tq.getResultList().isEmpty()) {
            res = tq.getResultList().get(0);
            log.debug("***** DAO LecturerO - lecturer found: {}", res);
        } else {
            res = null;
            log.debug("***** DAO LecturerO - lecturer NOT found: {}", res);
        }

        return res;
    }
}
